package com.flexrrb.resizeManagerClasses
{

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.containers.DividedBox;
	import mx.containers.DividerState;
	import mx.core.UIComponent;
	import mx.events.SandboxMouseEvent;

	public class ResizeBorder extends UIComponent
	{
		public function ResizeBorder(type:String)
		{
			super();
			borderType=type;
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		private var _borderType:String;
		private var knob:DisplayObject;

		public function get borderType():String
		{
			return _borderType;
		}

		public function set borderType(value:String):void
		{
			_borderType=value;
		}
		private var isMouseOver:Boolean=false;

		private function isVertical():Boolean
		{
			if (borderType == "left" || borderType == "right")
			{
				return true;
			}
			return false;
		}

		override public function set x(value:Number):void
		{
			var oldValue:Number=x;
			super.x=value;

			if (isVertical())
			{
				//DividedBox(owner).moveDivider(DividedBox(owner).getDividerIndex(this), value - oldValue);
			}
		}

		override public function set y(value:Number):void
		{
			var oldValue:Number=y;
			super.y=value;

			if (isVertical())
			{
				//DividedBox(owner).moveDivider(DividedBox(owner).getDividerIndex(this), value - oldValue);
			}
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if (isNaN(width) || isNaN(height))
				return;

			if (!parent)
				return;

			super.updateDisplayList(unscaledWidth, unscaledHeight);

			graphics.clear();

			graphics.beginFill(0x000000, 0);
			graphics.drawRect(0, 0, width, height);
			graphics.endFill();

			var color:Number;
			var alpha:Number=1.0;

			var thickness:Number=3;

			var gap:Number=0;

			if (state != DividerState.DOWN)
			{
				// Draw knob, if there is enough room
				if (gap >= 6)
				{
					if (!knob)
					{
						var knobClass:Class=Class(getStyle("dividerSkin"));
						if (knobClass)
							knob=new knobClass();
						if (knob)
							addChild(knob);
					}

					if (knob)
					{
						if (DividedBox(owner).isVertical())
						{
							knob.scaleX=1.0;
							knob.rotation=0;
						}
						else
						{
							// Rotate the knob
							knob.scaleX=-1.0;
							knob.rotation=-90;
						}

						knob.x=Math.round((width - knob.width) / 2);
						knob.y=Math.round((height - knob.height) / 2);
					}
				}
				return;
			}

			color=getStyle("dividerColor");
			alpha=getStyle("dividerAlpha");
			graphics.beginFill(color, alpha);

			if (DividedBox(owner).isVertical())
			{
				var visibleHeight:Number=thickness;

				if (visibleHeight > gap)
					visibleHeight=gap;

				var y:Number=(height - visibleHeight) / 2;
				graphics.drawRect(0, y, width, visibleHeight);
			}
			else
			{
				var visibleWidth:Number=thickness;

				if (visibleWidth > gap)
					visibleWidth=gap;

				var x:Number=(width - visibleWidth) / 2;
				graphics.drawRect(x, 0, visibleWidth, height);
			}

			graphics.endFill();
		}

		override public function styleChanged(styleProp:String):void
		{
			super.styleChanged(styleProp);

			if (!styleProp || styleProp == "dividerSkin" || styleProp == "styleName")
			{
				if (knob)
				{
					removeChild(knob);
					knob=null;
				}
			}
		}

		private function mouseOverHandler(event:MouseEvent):void
		{
			if (event.buttonDown)
				return;

			isMouseOver=true;
			if (!DividedBox(owner).activeDivider)
			{
				state=DividerState.OVER;
				DividedBox(owner).changeCursor(this);
			}
		}

		private function mouseOutHandler(event:MouseEvent):void
		{
			isMouseOver=false;
			if (!DividedBox(owner).activeDivider)
			{
				state=DividerState.UP;
				if (parent)
					DividedBox(owner).restoreCursor();
			}
		}

		private function mouseDownHandler(event:MouseEvent):void
		{
			DividedBox(owner).changeCursor(this);
			DividedBox(owner).startDividerDrag(this, event);

			var sbRoot:DisplayObject=systemManager.getSandboxRoot();
			sbRoot.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true);
			sbRoot.addEventListener(SandboxMouseEvent.MOUSE_UP_SOMEWHERE, mouseUpHandler);
		}

		private function mouseUpHandler(event:Event):void
		{
			if (!isMouseOver)
				DividedBox(owner).restoreCursor();

			state=DividerState.OVER;
			DividedBox(owner).stopDividerDrag(this, event as MouseEvent);

			var sbRoot:DisplayObject=systemManager.getSandboxRoot();
			sbRoot.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true);
			sbRoot.removeEventListener(SandboxMouseEvent.MOUSE_UP_SOMEWHERE, mouseUpHandler);
		}
	}

}
